package Algorithm;

//给定一个整数数组，有正有负，求子数组的最大和的值。如【3，2,-1,-7,2,7,-9,1】
//out：9。(2+7)
public class getMaxSum {
    public static void main(String[] args) {
        int arr[] = {3, 2, -1, -7, 2, 7, -9, 1};
        System.out.println("子数组的最大和的值:" + function(arr));
    }

    public static int function(int[] arr) {
        if (arr == null && arr.length == 0) {
            return 0;
        }
        int sumMax = arr[0];
        int currentMax = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (currentMax < 0) {
                currentMax = arr[i];
            } else {
                currentMax = currentMax + arr[i];
            }
            if (currentMax > sumMax) {
                sumMax = currentMax;
            }
        }
        return sumMax;
    }
}
